﻿function BangoAgent() {
	
}

BangoAgent.prototype.bangoLogger = new BangoLogger();
BangoAgent.prototype.bangoLoggingLevel = new BangoLoggingLevel();
BangoAgent.prototype.userAgent = null;
BangoAgent.prototype._logging_level_events = new Array();
BangoAgent.prototype._active_logging_level = new Array();
BangoAgent.prototype._logging_level = 0;
BangoAgent.prototype._events_list = new Array();


/**
 * The method used to distribute the application.
 *
 * @param applicationChannel
 *            The method used to distribute the application, for example
 *            "AppStore".
 */
BangoAgent.setApplicationChannel = function (applicationChannel) {
	_bangoAgent.bangoLogger.applicationChannel(applicationChannel);
};

/**
 * Gets the method used to distribute the application.
 *
 * @return The method used to distribute the application, for example
 *         "AppStore".
 */
BangoAgent.getApplicationChannel = function () {
	return _bangoAgent.bangoLogger.applicationChannel();
};

/**
* @return The maximum number of events to be added to each profile 
* when offline storage is in use. 0 indicates no maximum.
*/
BangoAgent.getMaximumEventsPerProfile = function () {
	return _bangoAgent.bangoLogger.maximumEventsPerProfile();
};

/**
@return the setting from Constants whether to identify the bango user ID
*/

BangoAgent.getIsSetToIdentifyBangoUser = function() {
	return _bangoAgent.bangoLogger.identifyBangoUser();
};

/**
@param setIdentifyBangoUser : set to true or false to identify 
BangoUser ID
*/

BangoAgent.setIdentifyBangoUser = function(setIdentifyBangoUser) {
	_bangoAgent.bangoLogger.identifyBangoUser(setIdentifyBangoUser);
};

/**
* @param maximumEventsPerProfile The maximum number of events to be added to each profile 
* when offline storage is in use. 0 indicates no maximum.
*/
BangoAgent.setMaximumEventsPerProfile = function (maximumEventsPerProfile) {
	_bangoAgent.bangoLogger.maximumEventsPerProfile(maximumEventsPerProfile);
};

/**
* @param postOnMaximumEvents Whether to post events when a profile reaches its maximum size. Default is false.
*/
BangoAgent.setPostOnMaximumEvents = function (postOnMaximumEvents) {
	_bangoAgent.bangoLogger.postOnMaximumEvents(postOnMaximumEvents);
};

/**
* @return Whether events are posted when a profile reaches its maximum size. Default is false.
*/
BangoAgent.getPostOnMaximumEvents = function () {
	return _bangoAgent.bangoLogger.postOnMaximumEvents();
};

/**
 * The type of application.
 *
 * @param applicationType
 *            The type of application, for example "Lite".
 */
BangoAgent.setApplicationType = function (applicationType) {
	_bangoAgent.bangoLogger.applicationType(applicationType);
};

/**
 * Gets the type of application.
 *
 * @return The type of application, for example "Lite"
 */
BangoAgent.getApplicationType = function () {
	return _bangoAgent.bangoLogger.applicationType();
};

/**
 * The application version to be used, if this is not supplied then the
 * application version number of the application is used.
 *
 * @param applicationVersion
 *            The application version to be used.
 */
BangoAgent.setApplicationVersion = function (applicationVersion) {
	_bangoAgent.bangoLogger.applicationVersion(applicationVersion);
};

/**
 * Gets the application version to be used.
 *
 * @return The application version to be used.
 */
BangoAgent.getApplicationVersion = function () {
	return _bangoAgent.bangoLogger.applicationVersion();
};

/**
 * The period in seconds, that after we receive onSessionEnd call we will
 * still count events in that of the previous session.
 *
 * @param continueSessionSeconds
 *            This is the period in seconds.
 */
BangoAgent.setContinueSessionSeconds = function (continueSessionSeconds) {
	_bangoAgent.bangoLogger.sessionResumePeriod(continueSessionSeconds);
};

/**
 * Gets the period in seconds, that after we receive onSessionEnd call we
 * will still count events in that of the previous session.
 *
 * @return The period in seconds.
 */
BangoAgent.getContinueSessionSeconds = function () {
	return _bangoAgent.bangoLogger.sessionResumePeriod();
};

/**
 * Whether or not an attempt should be made to discover the
 * user’s location, by default this is set to true.
 *
 * @param findLocation
 * 			Should location data be found.
 */
BangoAgent.setFindLocation = function (findLocation) {
	_bangoAgent.bangoLogger.findLocation(findLocation);
};

/**
 * Gets whether or not an attempt should be made to discover the user's
 * location.
 *
 * @return Whether or not an attempt should be made to discover the user's
 *         location.
 */
BangoAgent.getFindLocation = function () {
	return _bangoAgent.bangoLogger.findLocation();
};

/**
 * Enables or disables the logging of events.
 *
 * @param logEvents
 *            Should events be logged.
 */
BangoAgent.setLogEnabled = function (logEvents) {
	_bangoAgent.bangoLogger.logEvents(logEvents);
};

/**
 * Gets whether not events are being logged.
 *
 * @return Whether not events are being logged.
 */
BangoAgent.getLogEnabled = function () {
	return _bangoAgent.bangoLogger.logEvents();
};

/**
 * Sets the URL to post the data to.
 *
 * @param postURL
 *            The URL to post the data to.
 */
BangoAgent.setPostURL = function (postURL) {
	_bangoAgent.bangoLogger.postURL(postURL);
};

/**
 * Gets the URL to post the data to.
 *
 * @return The URL to post the data to.
 */
BangoAgent.getPostURL = function () {
	return _bangoAgent.bangoLogger.postURL();
};

/*
* Option removed for the use of localstorage to be optional, as this will be used on a website.
* If we did not use localstorage we would have to post at least one ajax request on each page. 
* This would slow down the users experience and potentially loose data that could not be sent.
*/


/**
 * The unique identifier that you have for the user of the application. For
 * example, this could be an email or a GUID or just a numerical value.
 *
 * @param userId
 */
BangoAgent.setUserId = function (userId) {
	_bangoAgent.bangoLogger.userId(userId);
};

/**
 * Gets the unique identifier for the user of the application
 *
 * @return The unique identifier for the user.
 */
BangoAgent.getUserId = function () {
	return _bangoAgent.bangoLogger.userId();
};

/**
 * If the user is roaming, a boolean set to false by default, but this method allows the you to override it.
 *
 * @param isRoaming (boolean)
 */
BangoAgent.setIsRoaming = function (isRoaming) {
	_bangoAgent.bangoLogger.isRoaming(isRoaming);
};

/**
 * Gets the whether the user is roaming
 *
 * @return if the user is roaming boolean, as this cannot be automatically detected it will default to false unless true passed in.
 */
BangoAgent.getIsRoaming = function () {
	return _bangoAgent.bangoLogger.isRoaming();
};


/**
 * Type of connection type used by the user, if the input is not passed in or not valid, as this cannot be automatically detected it will be defaulted to 'OTHER'
 *
 * @param connectionType ("WIFI", "OPERATOR" or "OTHER")
 */
BangoAgent.setConnectionType = function (connectionType) {
	_bangoAgent.bangoLogger.connectionType(connectionType);
};

/**
 * Gets the connection type used by the user.
 *
 * @return The connection type of the user, as this cannot be automatically detected it will be defaulted to 'OTHER' if no valid override is passed in
 */
BangoAgent.getConnectionType = function () {
	return _bangoAgent.bangoLogger.connectionType();
};

/**
 * The Operator's name eg 'Sprint'
 *
 * @param operator's name
 */
BangoAgent.setOperatorName = function (operatorName) {
	_bangoAgent.bangoLogger.operatorName(operatorName);
};

/**
 * Gets the operator's name if it has been passed in
 *
 * @return operator's name
 */
BangoAgent.getOperatorName = function () {
	return _bangoAgent.bangoLogger.operatorName();
};

/**
 * The country 2 or 3 character ISO code
 *
 * @param countryISO
 */
BangoAgent.setCountryISO = function (countryISO) {
	_bangoAgent.bangoLogger.countryISO(countryISO);
};

/**
 * Gets the countryISO code if it has been passed in
 *
 * @return countryISO
 */
BangoAgent.getCountryISO = function () {
	return _bangoAgent.bangoLogger.countryISO();
};

/**
 * The brand of the device. For example, Sprint, Verizon
 *
 * @param deviceBrand
 */
BangoAgent.setDeviceBrand = function (deviceBrand) {
	_bangoAgent.bangoLogger.deviceBrand(deviceBrand);
};

/**
 * The brand of the device. For example, Sprint, Verizon
 *
 * @return deviceBrand
 */
BangoAgent.getDeviceBrand = function () {
	return _bangoAgent.bangoLogger.deviceBrand();
};

/**
 * The make of the device. For example, HTC, Apple
 *
 * @param deviceMake
 */
BangoAgent.setDeviceMake = function (deviceMake) {
	_bangoAgent.bangoLogger.deviceMake(deviceMake);
};

/**
 * The make of the device. For example, HTC, Apple
 *
 * @return deviceMake
 */
BangoAgent.getDeviceMake = function () {
	return _bangoAgent.bangoLogger.deviceMake();
};

/**
 * The model of the device. For example, Desire, iPhone 4
 *
 * @param deviceModel
 */
BangoAgent.setDeviceModel = function (deviceModel) {
	_bangoAgent.bangoLogger.deviceModel(deviceModel);
};

/**
 * The model of the device. For example, Desire, iPhone 4
 *
 * @return deviceModel
 */
BangoAgent.getDeviceModel = function () {
	return _bangoAgent.bangoLogger.deviceModel();
};

/**
 * The version of the device. For example, 1.1
 *
 * @param deviceVersion
 */
BangoAgent.setDeviceVersion = function (deviceVersion) {
	_bangoAgent.bangoLogger.deviceVersion(deviceVersion);
};

/**
 * The version of the device. For example, 1.1
 *
 * @return deviceVersion
 */
BangoAgent.getDeviceVersion = function () {
	return _bangoAgent.bangoLogger.deviceVersion();
};

/**
 * The name of the operating system. For example, Android
 *
 * @param operatingSystem
 */
BangoAgent.setOperatingSystem = function (operatingSystem) {
	_bangoAgent.bangoLogger.operatingSystem(operatingSystem);
};

/**
 * The name of the operating system. For example, Android
 *
 * @return operatingSystem
 */
BangoAgent.getOperatingSystem = function () {
	return _bangoAgent.bangoLogger.operatingSystem();
};

/**
 * The version of the operating system. For example, 1.1
 *
 * @param operatingSystemVersion
 */
BangoAgent.setOperatingSystemVersion = function (operatingSystemVersion) {
	_bangoAgent.bangoLogger.operatingSystemVersion(operatingSystemVersion);
};

/**
 * The version of the operating system. For example, 1.1
 *
 * @return operatingSystemVersion
 */
BangoAgent.getOperatingSystemVersion = function () {
	return _bangoAgent.bangoLogger.operatingSystemVersion();
};

/**
 * The wireless standard that the mobile connection uses. As we cannot detect this value, it will be set to ‘OTHER’ if no value is passed in. 
 * See the integration guide for the list of valid values.
 *
 * @param operatorConnectionType
 */
BangoAgent.setOperatorConnectionType = function (operatorConnectionType) {
	_bangoAgent.bangoLogger.operatorConnectionType(operatorConnectionType);
};

/**
 * The wireless standard that the mobile connection uses. As we cannot detect this value, it will be set to ‘OTHER’ if no value is passed in. 
 * See the integration guide for the list of valid values.
 *
 * @return operatorConnectionType
 */
BangoAgent.getOperatorConnectionType = function () {
	return _bangoAgent.bangoLogger.operatorConnectionType();
};

BangoAgent.onStartSession = function (applicationKey, eventValue, eventDetail, eventParameters) {
	if (!(applicationKey) || (applicationKey == "")) {
		throw "Api key not specified";
		return;
	}
	
	_bangoAgent.bangoLogger.initLogger(applicationKey);
	_bangoAgent.bangoLogger.postEventsNow(false, true);
	_bangoAgent.bangoLogger.logEventStart(eventValue, eventDetail, eventParameters);
};

BangoAgent.onEndSession = function (forceEventPosting, eventValue, eventDetail, eventParameters) {
	try {
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.SESSION_END(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}

	if (forceEventPosting) {
		_bangoAgent.bangoLogger.postEventsNow(true, false);
	}
};

BangoAgent.onPageView = function (eventValue, eventDetail, eventParameters) {
	try {
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.EVENT_PAGEVIEW(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onSearch = function (eventValue, eventDetail, eventParameters) {
	try {
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.EVENT_SEARCH(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onLiveVideoStart = function (eventValue, eventDetail, eventParameters) {
	try {
		if (eventParameters == null) {
			eventParameters = new Array();
		}
		eventParameters.push(new BangoEventParameter(BangoConstants.VIDEO_PARAMETERNAME(), BangoConstants.VIDEO_LIVEPARAMETER()));
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.EVENT_VIDEOSTART(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onLiveVideoStop = function (eventValue, eventDetail, eventParameters) {
	try {
		if (eventParameters == null) {
			eventParameters = new Array();
		}
		eventParameters.push(new BangoEventParameter(BangoConstants.VIDEO_PARAMETERNAME(), BangoConstants.VIDEO_LIVEPARAMETER()));
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.EVENT_VIDEOSTOP(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onDemandVideoStart = function (eventValue, eventDetail, eventParameters) {
	try {
		if (eventParameters == null) {
			eventParameters = new Array();
		}
		eventParameters.push(new BangoEventParameter(BangoConstants.VIDEO_PARAMETERNAME(), BangoConstants.VIDEO_ONDEMANDPARAMETER()));
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.EVENT_VIDEOSTART(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onDemandVideoStop = function (eventValue, eventDetail, eventParameters) {
	try {
		if (eventParameters == null) {
			eventParameters = new Array();
		}
		eventParameters.push(new BangoEventParameter(BangoConstants.VIDEO_PARAMETERNAME(), BangoConstants.VIDEO_ONDEMANDPARAMETER()));
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.EVENT_VIDEOSTOP(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onIdle = function (eventValue, eventDetail, eventParameters) {
	try {
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.SESSION_IDLE_START(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onResume = function (eventValue, eventDetail, eventParameters) {
	try {
		_bangoAgent.bangoLogger.logEvent(BangoSessionEvents.SESSION_IDLE_END(), eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onEvent = function (eventName, eventValue, eventDetail, eventParameters) {
	try {
		_bangoAgent.bangoLogger.logEvent(eventName, eventValue, eventDetail, eventParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorWarning(ex);
	}
};

BangoAgent.onError = function (errorId, message, errorClass, errorParameters) {
	try {
		_bangoAgent.bangoLogger.logEventError(errorId, message, errorClass, errorParameters);
	}
	catch (ex) {
		_bangoAgent.bangoLogger.logInternalErrorCritical(ex);
	}
};

BangoAgent.postEvents = function () {
	_bangoAgent.bangoLogger.postEventsNow(true, true);
};

BangoAgent.viewBatch = function () {
	return JSON.stringify(BangoStorage.getBatch());
};

//Logging Levels
/**
 * Defines the events list for the given logging level
 */
BangoAgent.setLoggingLevelEvents = function (ValuesCachedLoggingLevel, eventsList) {
	_bangoAgent._logging_level_events[0] = ["ALL"];
	var index = BangoAgent.getOrdinalIndex(ValuesCachedLoggingLevel.value);
	if ((-1 < index) && (index < _bangoAgent.bangoLoggingLevel.ValuesCached.TOTAL_LEVELS.value)) {
		_bangoAgent._logging_level_events[index] = eventsList;
	}
};

/**
* Sets which the logging level use
* 
* @param loggingLevel
*           //pass in a logging level i.e _bangoAgent.bangoLoggingLevel.ValuesCached.LEVEL_1
*/
BangoAgent.setLoggingLevel = function(ValuesCachedLoggingLevel) {
	_bangoAgent._logging_level = ValuesCachedLoggingLevel.value;
	_bangoAgent._active_logging_level = BangoAgent.getOrdinalIndices(ValuesCachedLoggingLevel.value);
	
};

/**
* Gets value of the current logging level
* @return
*/

BangoAgent.getLoggingLevel = function() {
	return _bangoAgent._logging_level;
};

BangoAgent.EventNameAllowed = function (eventName) {
	var eventsList = new Array();
	var logAllowed = true;
	if (_bangoAgent._active_logging_level.length > 0) {
		logAllowed = (eventName == BangoSessionEvents.SESSION_START()) || (eventName == BangoSessionEvents.SESSION_END()) || (eventName == BangoSessionEvents.SESSION_IDLE_START()) || (eventName == BangoSessionEvents.SESSION_IDLE_END());
		if (!logAllowed) {
			for (var index in _bangoAgent._active_logging_level) {
				eventsList.push(_bangoAgent._logging_level_events[_bangoAgent._active_logging_level[index]]);
				if (eventsList.length > 0) {
					for (var i = 0; i < eventsList.length; i++) {
						var event = eventsList[i];
						for (var j = 0; j < event.length; j++) {
							if (eventName == event[j]) {
								return true;
							}
						}
					}
				}
			}
		}
	}

};

//pass in a logging level i.e _bangoAgent.bangoLoggingLevel.ValuesCached.LEVEL_1.value
BangoAgent.getOrdinalIndex = function (value) {
	var loggingLevelList = BangoAgent.getOrdinalIndices(value);
	if (loggingLevelList.length == 0) {
		return -1;
	}
	return loggingLevelList[0];
};
//pass in integer representing the logging level value, i.e 8 for logging level 4
BangoAgent.getOrdinalIndices = function(loggingLevel) {
	var indices = new Array();
	var levelAll = _bangoAgent.bangoLoggingLevel.ValuesCached.ALL.value;
	
	for (var i in _bangoAgent.bangoLoggingLevel.ValuesCached) {
		if (_bangoAgent.bangoLoggingLevel.ValuesCached[i].value != levelAll) {
			var value = _bangoAgent.bangoLoggingLevel.ValuesCached[i].value;
			if ((loggingLevel & value) == value) {
				indices.push(value);
			}
		}
	}
	return indices;
};

var _bangoAgent = new BangoAgent();